package render

import (
	"MetroBuilderGameJam/gameEngine/drawer"
	"MetroBuilderGameJam/gameEngine/geom"
	"MetroBuilderGameJam/gameEngine/utils"
	"MetroBuilderGameJam/guiElement"
	"github.com/hajimehoshi/ebiten/v2"
	"image/color"
)

func NewButtonRenderer(d *drawer.Drawer, allowedSize geom.IntSize) *ButtonRenderer {
	return &ButtonRenderer{
		drawer:       d,
		guiPanelSize: allowedSize,
	}
}

type ButtonRenderer struct {
	drawer       *drawer.Drawer
	guiPanelSize geom.IntSize
}

type GuiDrawable interface {
	IsActive() bool
	Position() geom.Point
	Size() geom.Size
	GetPurpose() guiElement.Purpose
	GetText() string
	GetColor() color.Color
}

func (b *ButtonRenderer) Draw(screen *ebiten.Image, item GuiDrawable) *utils.Figure {
	if item == nil {
		return utils.NoFigure()
	}
	return utils.NewFigure(item, b.drawButtonTextAsShape(screen, item))
}

func (b *ButtonRenderer) drawButtonTextAsShape(screen *ebiten.Image, item GuiDrawable) geom.Collider {
	var clr color.Color = drawer.ColorGray
	if !item.IsActive() {
		clr = drawer.ColorTransparent(clr, 0.3)
	}

	pos := item.Position().AsVec2()
	if pos.X < 0 {
		pos.X = float64(b.guiPanelSize.Width) + pos.X
	}
	if pos.Y < 0 {
		pos.Y = float64(b.guiPanelSize.Height) + pos.Y
	}

	switch item.GetPurpose() {
	case guiElement.ButtonPause:
		w := item.Size().Width / 3
		size := geom.Size{Width: w, Height: item.Size().Height}
		b.drawer.FillRectangle(screen, pos.Sub(geom.NewVec2(w*3/2, 0)).AsPoint(), size, 0, clr)
		b.drawer.FillRectangle(screen, pos.AsPoint(), size, 0, drawer.ColorTransparent(clr, 0))
		b.drawer.FillRectangle(screen, pos.Add(geom.NewVec2(w*3/2, 0)).AsPoint(), size, 0, clr)

		return geom.NewColliderRectangle(b.drawer.SimpleRectangleToDrawed(pos.AsPoint(), item.Size()))
	case guiElement.ButtonSpeed1:
		b.drawer.FillArrowRight(screen, pos.AsPoint(), geom.NewVec2(0, 0), item.Size(), 0, clr)

		return geom.NewColliderRectangle(b.drawer.SimpleRectangleToDrawed(pos.AsPoint(), item.Size()))
	case guiElement.ButtonSpeed2:
		size := item.Size().AsVec2().Mul(0.8).ToSize()
		b.drawer.FillArrowRight(screen, pos.AsPoint(), geom.NewVec2(-size.Width*2/3, 0), size, 0, clr)
		b.drawer.FillArrowRight(screen, pos.AsPoint(), geom.NewVec2(size.Width, 0), size, 0, clr)

		return geom.NewColliderRectangle(b.drawer.SimpleRectangleToDrawed(pos.AsPoint(), item.Size()))

	case guiElement.ButtonTrain:
		r := geom.Degrees(-90).ToRadians()
		stern := b.drawer.FillSoftArrowRight(screen, pos.AsPoint(), geom.NewVec2(item.Size().Width, 0), item.Size(), r, clr)
		vehicle := b.drawer.FillRectangle(screen, pos.AsPoint(), item.Size(), r, clr)
		_, _ = stern, vehicle
		p := pos.Add(item.Size().AsVec2()).AsPoint()
		b.drawer.FillCircle(screen, p, 8, drawer.ColorBlack)
		if len(item.GetText()) > 1 {
			b.drawer.FillCircle(screen, p.AsVec2().Add(geom.NewVec2(5, 0)).AsPoint(), 8, drawer.ColorBlack)
		}
		b.drawer.Text(screen, item.GetText(), p, drawer.ColorWhite)

		return geom.NewColliderRectangle(b.drawer.SimpleRectangleToDrawed(pos.AsPoint(), geom.Size{Width: item.Size().Width, Height: item.Size().Height + 5}))

	case guiElement.ButtonLine:
		if item.GetColor() != nil && item.IsActive() {
			clr = item.GetColor()
		} else {
			clr = drawer.ColorGray
		}
		b.drawer.FillCircle(screen, pos.AsPoint(), item.Size().Width, clr)
		return geom.NewColliderCircle(pos.AsPoint(), item.Size().Width)

	case guiElement.MenuButton:
		bg := drawer.ColorBlue
		length := float64(len([]rune(item.GetText())))
		b.drawer.BorderedSimpleRectangle(screen, pos.Sub(geom.NewVec2(length*2, item.Size().Height).Div(4)).AsPoint(), item.Size(), 3, clr, bg)
		b.drawer.Text(screen, item.GetText(), pos.Add(geom.NewVec2(8, 5)).AsPoint(), drawer.ColorWhite)

		return geom.NewColliderRectangle(b.drawer.SimpleRectangleToDrawed(pos.AsPoint(), geom.Size{Width: item.Size().Width, Height: item.Size().Height + 5}))
	}

	return &geom.ColliderPoint{Pivot: item.Position()}
}
